package com.hk.service.impl;

import com.hk.dao.ICompanyMessageDao;
import com.hk.dao.IContactUsDao;
import com.hk.dao.impl.CompanyMessageDaoImpl;
import com.hk.dao.impl.ContactUsDaoImpl;
import com.hk.model.ContactUs;
import com.hk.service.IContactUsService;
import com.hk.utils.JdbcMysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/20 17:39
 * State：联系我们业务逻辑层
 */
public class ContactUsServiceImpl implements IContactUsService {

    IContactUsDao iContactUsDao = new ContactUsDaoImpl();

    @Override
    public List<ContactUs> findAll(Integer page, Integer limit) {
        ResultSet resultSet = iContactUsDao.findAll(page, limit);
        List<ContactUs> contactUsList = transverter(resultSet);
        if (contactUsList == null) {
            contactUsList = new ArrayList<>();
        }
        //        关闭资源
        JdbcMysql.jdbcClose();
        return contactUsList;
    }

    @Override
    public int count(Integer page, Integer limit) {
        ResultSet resultSet = iContactUsDao.count(page,limit);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            return count;
        } catch (SQLException e) {
            e.getMessage();
        }
        //        关闭资源
        JdbcMysql.jdbcClose();
        return count;
    }

    @Override
    public ContactUs findById(Integer id) {
        ResultSet resultSet = iContactUsDao.findById(id);
        List<ContactUs> contactUsList = transverter(resultSet);
        //        关闭资源
        JdbcMysql.jdbcClose();
        return contactUsList.get(0);
    }

    @Override
    public int save(ContactUs t) {
        return 0;
    }

    @Override
    public int update(ContactUs t) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    /**
     * 将传递的数据进行转换成集合
     * @param resultSet
     * @return
     */
    public List<ContactUs> transverter(ResultSet resultSet ) {
        List<ContactUs> contactUsList = new ArrayList<>();
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("site");
                String phone = resultSet.getString("phone");
                String fax = resultSet.getString("fax");
                String email = resultSet.getString("email");
                String url = resultSet.getString("url");
                int state = resultSet.getInt("state");
                String gmtCreate = resultSet.getString("gmt_create");
                String gmtModified = resultSet.getString("gmt_modified");
//                封装对象
                ContactUs contactUs = new ContactUs(id,name,phone,fax,email,url,state,gmtCreate,gmtModified);
                contactUsList.add(contactUs);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return contactUsList;

    }

}
